package com.example.demo.controller;

import com.example.demo.Server.UserService;
import com.example.demo.constant.Constants;
import com.example.demo.model.Result;
import com.example.demo.model.UserInfo;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/login",produces = "application/json")
    public Result login(String userName, String password, HttpSession session){
        //1.
        if(!StringUtils.hasLength(userName)|| !StringUtils.hasLength(password)){
            return Result.fail("用户名或密码不能为空!");
        }
        //2.从数据库中拿数据,进行校验
       /* if(!"zhangsan".equals(userName) ||!"123456".equals(password)){
            return "用户名或密码错误!";
        }*/
//        if(!password.equals(userService.queryByName(userName))){
//            return "用户名或密码错误!";
//        }

        UserInfo userInfo = userService.queryByName(userName);
        if (userInfo==null || userInfo.getId()<0){
            return Result.fail("用户不存在啊!");
        }
        if (!password.equals(userInfo.getPassword())){
            return Result.fail("用户名或密码错误!");
        }

        //存储Session
        userInfo.setPassword("");
        session.setAttribute(Constants.USER_SESSION_KEY, userInfo);

        return Result.success("");

    }
}
